package top.starmc.starquesthelper.config

import org.bukkit.Bukkit
import taboolib.common.LifeCycle
import taboolib.common.platform.Awake
import taboolib.common.platform.function.submitAsync
import taboolib.module.configuration.Config
import taboolib.module.configuration.ConfigFile
import top.starmc.starquesthelper.data.QuestData
import top.starmc.starquesthelper.util.syncQuest

object Config {
    @Config("config.yml")
    lateinit var config: ConfigFile

    @Config("quest.yml")
    lateinit var quest: ConfigFile

    val questMap = HashMap<String, QuestData>()

    @Awake(LifeCycle.ENABLE)
    fun loadConfig() {
        questMap.clear()

        quest.reload()

        quest.getKeys(false).forEach {
            questMap[it] = QuestData(quest.getConfigurationSection(it)!!)
        }
    }

    @Awake(LifeCycle.ENABLE)
    fun autoSync() {
        val period = config.getLong("auto-sync", -1L)
        if (period != -1L) {
            submitAsync(delay = period, period = period) {
                Bukkit.getOnlinePlayers().forEach {
                    it.syncQuest()
                }
            }
        }
    }
}